﻿Imports System.Data.SqlClient

Public Class FormProductEditor

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If txtProductId.Text = "" Then
            Dim cn As New SqlConnection()
            cn.ConnectionString = "server=.;uid=sa;pwd=123;database=SaleDb;"
            cn.Open()

            Dim cmd As New SqlCommand()
            cmd.Connection = cn
            cmd.CommandText = "INSERT INTO Product VALUES(N'" & txtProductCode.Text & "',N'" & txtProductName.Text & "',N'" & txtCategoryId.Text & "',N'" & txtPrice.Text & "',N'" & txtMinimunPrice.Text & "',1)"
            cmd.ExecuteNonQuery()
        Else
            Dim cn As New SqlConnection()
            cn.ConnectionString = "server=.;uid=sa;pwd=123;database=SaleDb;"
            cn.Open()

            Dim cmd As New SqlCommand()
            cmd.Connection = cn

            Dim query As String = "UPDATE Product set ProductCode ='" + txtProductCode.Text + "',ProductName= '" + txtProductName.Text + "',CategoryId=" + txtCategoryId.Text + ",Price =" + txtPrice.Text + ",MinimumPrice =" + txtMinimunPrice.Text + ",Acitve =1 where ProductId = " + txtProductId.Text + ";"

            cmd.CommandText = query
            cmd.ExecuteNonQuery()

        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub txtProductCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtProductCode.TextChanged

    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
End Class
